import Mock from "mockjs"
let { data } = Mock.mock({
    // 20条数据
    "data|120": [{
        "id|+1": 1,
        // 房间号
        "roomId|1000-9999": 1000,
        // 主播ID
        "anchorId|1-99999": 1,
        //主播名称
        "name": "@cname(6)",
        "state|1": ['直播中','已结束'],
        //主播分类
        "anchorType|1": ["女装", "舞蹈", "颜值", "游戏"],
        //时间 
        "times": "@time",
        //观看人数
        "peopNum|0-100000": 0,
        //操作
        "operation": "主播动态",
    }]
})
// 输出结果随机生成的数据（node index.js）
console.log(data);


// 列表接口，分页，搜索
Mock.mock("/api/pos/list", "post", (options: any) => {
    const { pageSize, currentPage, search } = JSON.parse(options.body);

    if (pageSize && currentPage) {
        let result;
        let count = 0;
        if (search) {
            result = data
                .filter((item: any) => item.doctor === search)
                .slice((currentPage - 1) * pageSize, currentPage * pageSize);
            count = data.filter((item: any) => item.doctor === search).length;
        } else {
            result = data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
            count = data.length;
        }
        return {
            status: 0,
            result: result,
            count: count,
        };
    } else {
        return {
            status: 1,
            result: "参数错误",
        };
    }
});

// 删除接口
Mock.mock("/api/pos/del", "post", (options: any) => {
    const { id } = JSON.parse(options.body);
    if (id) {
        data = data.filter((item: any) => item.id !== id);
        return {
            status: 0,
            msg: "删除成功",
        };
    } else {
        return {
            status: 1,
            msg: "参数错误",
        };
    }
});

export default Mock;